Exercise 21

Author

Nick Beegle

#install.packages("dataRetrieval")
#install.packages("tsibble")
#install.packages("plotly")
#install.packages("feasts")
library(dataRetrieval)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(lubridate)

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
library(tsibble)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'
The following object is masked from 'package:lubridate':

    interval
The following objects are masked from 'package:base':

    intersect, setdiff, union
library(ggplot2)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(feasts)
Loading required package: fabletools
library(fabletools)
# Download daily streamflow data from USGS
poudre_flow <- readNWISdv(siteNumber = "06752260",
                          parameterCd = "00060",
                          startDate = "2013-01-01",
                          endDate = "2023-12-31") |>
  renameNWISColumns() |>
  mutate(Date = yearmonth(Date)) |>
  group_by(Date) |>
  summarise(Flow = mean(Flow, na.rm = TRUE))
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31
#convert to tsibble
poudre_ts <- as_tibble(poudre_flow, index = date)
#plot
flow_plot <- ggplot(poudre_ts, aes(x = Date, y = Flow)) +
  geom_line(color = "blue") +
  labs(title = "Cache la Poudre Monthly Streamflow",
       x = "Date",
       y = "Flow (cfs)") +
  theme_minimal()

ggplotly(flow_plot)
#animated plot with plotly
poudre_ts_anim <- poudre_ts |> 
  mutate(frame = as.character(Date))

plot_ly(poudre_ts_anim, 
        x = ~Date, 
        y = ~Flow, 
        type = 'scatter', 
        mode = 'lines',
        frame = ~frame,
        line = list(color = 'blue')) |>
  layout(title = "Animated Streamflow Time Series",
         xaxis = list(title = "Date"),
         yaxis = list(title = "Flow (cfs)")) |>
  animation_opts(frame = 100, redraw = TRUE)
#subseries

poudre_ts <- poudre_flow |>
  mutate(Date = yearmonth(Date)) |>
  group_by(Date) |>
  summarise(Flow = mean(Flow, na.rm = TRUE)) |>
  as_tsibble(index = Date)

gg_subseries(poudre_ts, Flow)

Interpretation: Seasons are defined as months in this plot. Each sub series depicts the variation in flow for each month over different years. There are peaks when snow melts and lows in dryer or colder times of the year.

#STL decomposition
decomposition <- poudre_ts |> 
  model(STL(Flow ~ season(window = "periodic"))) |> 
  components()

autoplot(decomposition)

There is long term changes in flow possibly due to climate change or land use changes. This can be seen in the trend section of the graph. Also seasonal changes can be seen in the peaks in flow throughout the year with peaks and valleys. The remainder captures anything irregular with the data.